package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.TblDataRockNegativeFeedback;
import com.hllxd.hism.star.basic.service.service.NegativeFeedbackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author wuguangxi
 * @description
 * @date 2023/12/27 15:28
 **/
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/negative_feedback")
public class NegativeFeedbackController2 {

    @Autowired
    private NegativeFeedbackService negativeFeedbackService;

    @RestGrpcMapping("/list.do")
    public NTSTATUS list(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            List<TblDataRockNegativeFeedback> items = negativeFeedbackService.findAll();
            log.info("items: {}.", items.size());
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/save.do")
    public NTSTATUS save(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            TblDataRockNegativeFeedback data = params.driverObject(TblDataRockNegativeFeedback.class);
            String userName = params.userID;
            data.setTechOa(userName);
            if(data.getId() == null){
                negativeFeedbackService.save(data);
            }else {
                negativeFeedbackService.modify(data);
            }
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/delete.do")
    public NTSTATUS delete(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            TblDataRockNegativeFeedback data = params.driverObject(TblDataRockNegativeFeedback.class);
            negativeFeedbackService.delete(data.getId());
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/detail.do")
    public NTSTATUS detail(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            TblDataRockNegativeFeedback data = params.driverObject(TblDataRockNegativeFeedback.class);
            result.setData(negativeFeedbackService.findById(data.getId()));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }
}
